﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using NENAData.Factory;
using NENAData.LINQ;

namespace NENAData.Transactions.Payments
{
    public class Payments
    {
        private static MembershipDataContext GetMembershipDB()
        {
            var x = new SQLMembership();
            return x.GetMembershipDB();
        }

        public List<TransactionPayment> GetPaidby(NENAData.Payments.NENAPayments.PaidBy oPaidBy)
        {
            MembershipDataContext db = GetMembershipDB();

            var listTP = (from tp in db.TransactionPayments
                          where tp.PaymentType == oPaidBy.ToString() &&
                          tp.TransactionCompleted
                          orderby tp.Member_ID
                          select tp).ToList();

            return listTP;
        }

        public List<TransactionPayment> GetTransactionPayments(int intMemberID)
        {
            MembershipDataContext db = GetMembershipDB();

            var listTP = (from tp in db.TransactionPayments
                          where tp.Member_ID == intMemberID
                          orderby tp.PaymentDate 
                          select tp).ToList();

            return listTP;
        }

        public List<TransactionPayment> GetTransactionPaymentsbyAffiliate(int intAffiliateID)
        {
            MembershipDataContext db = GetMembershipDB();

            var listTP = (from tp in db.TransactionPayments
                          where tp.PaymentTo == intAffiliateID
                          orderby tp.PaymentDate descending 
                          select tp).ToList();
            //                          &    tp.PaymentStatus == "Completed"

            return listTP;
        }

        public TransactionPayment GetTransactionPaymentsbyPayPalTransactionId(string txnId)
        {
            MembershipDataContext db = GetMembershipDB();

            var listTP = (from tp in db.TransactionPayments
                          where tp.TransactionID_PayPal == txnId
                          orderby tp.PaymentDate descending
                          select tp).FirstOrDefault();
            //                          &    tp.PaymentStatus == "Completed"

            return listTP;
        }

        public TransactionPayment GetTransactionPayment_byTransactionPaymentID(int intTransactionPaymentID)
        {
            MembershipDataContext db = GetMembershipDB();
            //http://stackoverflow.com/questions/315966/how-do-you-construct-a-linq-to-entities-query-to-load-child-objects-directly-ins
            DataLoadOptions options = new DataLoadOptions();
            options.LoadWith<TransactionPayment>(ii => ii.Transaction);
            TransactionPayment oTransactionPayment = (from tp in db.TransactionPayments
                                                  where tp.TransactionPaymentID == intTransactionPaymentID
                                           select tp).Single();

            return oTransactionPayment;
        }

    }
}
